<template>
  <div class="simple-profile">
    <h2>个人中心</h2>
    
    <div class="profile-content">
      <!-- 头像区域 -->
      <div class="avatar-box">
        <el-upload
          class="avatar-uploader"
          :action="'api/users/avatar_upload?id='+user.id"
          :show-file-list="false"
          :on-success="uploadSuccess"
          :before-upload="beforeUpload"
        >
          <img v-if="user.imgUrl" :src="serverUrl+user.imgUrl" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
        <p>点击更换头像</p>
      </div>
      
      <!-- 信息区域 -->
      <div class="info-box">
        <div class="info-item">
          <span>账号：</span>
          <span>{{ user.account }}</span>
        </div>
        <div class="info-item">
          <span>用户组：</span>
          <span>{{ user.userGroup }}</span>
        </div>
        <div class="info-item">
          <span>注册时间：</span>
          <span>{{ formatDate(user.ctime) }}</span>
        </div>
        
        <div class="buttons">
          <el-button type="primary" size="small" @click="editInfo">编辑资料</el-button>
          <el-button size="small" @click="changePwd">修改密码</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { ElMessage } from 'element-plus';
import { Plus } from '@element-plus/icons-vue';
import { accountInfo, uploadAvatar } from '@/api/account';
import dayjs from 'dayjs';
import { serverUrl } from '@/utils/common';
import { useUserStore } from '@/stores/userStore';

const user1 = JSON.parse(sessionStorage.getItem('user'));
const user = ref({});
const store = useUserStore();

// 调用获取账号（个人中心）信息接口
const getAccountInfo = async () => {
  try {
    const res = await accountInfo({
      id: user1.id
    });
    user.value = res.accountInfo;
    console.log(user.value);
  } catch (error) {
    console.error('获取用户信息失败', error);
    ElMessage.error('获取用户信息失败');
  }
};

getAccountInfo();

// 时间格式化
const formatDate = (date) => {
  if (!date) return '';
  return dayjs(date).format('YYYY-MM-DD');
};

// 头像上传前的检查
const beforeUpload = (file) => {
  if (!file.type.includes('image')) {
    ElMessage.error('请上传图片文件');
    return false;
  }
  if (file.size > 1024 * 1024 * 2) { // 2MB
    ElMessage.error('图片大小不能超过 2MB');
    return false;
  }
  return true;
};

// 头像上传成功后的处理
const uploadSuccess = () => {
  getAccountInfo();
  store.changeUserInfoState(); // 修改 Pinia 中状态 表示我已经修改了头像
};

const editInfo = () => {
  ElMessage.info('编辑资料');
};

const changePwd = () => {
  ElMessage.info('修改密码');
};
</script>

<style scoped>
.simple-profile {
  max-width: 600px;
  margin: 20px auto;
  padding: 20px;
}

h2 {
  text-align: center;
  margin-bottom: 20px;
}

.profile-content {
  display: flex;
  gap: 30px;
  padding: 20px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.avatar-box {
  text-align: center;
}

.avatar {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  object-fit: cover;
}

.avatar-icon {
  font-size: 40px;
  color: #999;
  background: #f5f5f5;
  padding: 40px;
  border-radius: 50%;
}

.info-box {
  flex: 1;
}

.info-item {
  margin-bottom: 15px;
}

.info-item span:first-child {
  color: #666;
  margin-right: 10px;
}

.buttons {
  margin-top: 25px;
}

@media (max-width: 600px) {
  .profile-content {
    flex-direction: column;
    align-items: center;
  }
}
</style>